package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Function;
import edu.chinasoft.domain.Role;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.FunctionService;
import edu.chinasoft.service.RoleService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myUpdateRolePageServlet", value = "/servlet/updaterolepageservlet")
public class UpdateRolePageServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(UpdateRolePageServlet.class);
	private RoleService roleService;
	private FunctionService functionService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" Servelt01 >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		RoleService roleServiceImp = wac.getBean("roleService", RoleService.class);
		this.roleService = roleServiceImp;
		//获取当前角色的id
		String num = request.getParameter("id");
		
		int id = Integer.parseInt(num);
		Role role = new Role();
		role.setRoleId(id);
		//通过id获取当前角色的其他信息
		Role selectRoleByRoleId =roleService.selectRoleByRoleId(role);
		logger.info("当前角色的信息为："+selectRoleByRoleId);
		String functions="";
		List<Function> functionList = selectRoleByRoleId.getFunctionList();
		for(Function function:functionList) {
			functions+=function.getFunctionName()+"、";
		}
		functions=functions.substring(0, functions.length()-1);
		logger.info("当前权限为："+functions);
		HttpSession session = request.getSession();
		session.setAttribute("functionNames", functions);
		session.setAttribute("updaterole", selectRoleByRoleId);
		List<Function> allFunctionList = functionService.selectFunction();
		session.setAttribute("allFunction", allFunctionList);
		response.sendRedirect(""+request.getContextPath()+"/updaterole.jsp");
		logger.info(" Servelt01 >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" Servelt01 >doPost start.......");
		this.doGet(request, response);
		logger.info(" Servelt01 >doPost end.......");
	}

}